package cn.edu.buaa.txtReader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import util.Global;

/**
 * Created by Administrator on 2016/3/9.
 */
public class FileAdapter extends BaseAdapter {
    private Context ctx;
    private List<Map<String, Object>> allValues = new ArrayList<Map<String, Object>>();

    public FileAdapter(Context ctx, List<Map<String, Object>> allValues) {
        this.ctx = ctx;
        this.allValues = allValues;
    }

    @Override
    public int getCount() {
        return allValues.size();
    }

    @Override
    public Object getItem(int position) {
        return allValues.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(ctx).inflate(R.layout.content_main, null);
            //设置高度
            convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Global.SCREEN_HEIGHT / 9));
        }
        //取得组件
        TextView fileImg = (TextView) convertView.findViewById(R.id.file_img);
        fileImg.getLayoutParams().height = Global.SCREEN_HEIGHT / 9;
        TextView fileName = (TextView) convertView.findViewById(R.id.file_name);

        //取得数据，设置到组件里
        Map<String, Object> map = allValues.get(position);

        //设置内容，文字
        fileName.setText(map.get("fileName").toString());

        //图片要根据扩展名取得
        String extName = map.get("extName").toString();

        //取得图片的id
        int imgId = Global.allIconImgs.get(extName);

        //设置图片
        fileImg.setBackgroundResource(imgId);

        //设置图片，根据传入的扩展名，在Globals中的map集合里查找符合该扩展名对应的文件图标，并设置这张图。
        fileImg.setBackgroundResource(Global.allIconImgs.get(map.get("extName").toString()));
        return convertView;
    }
}
